<template>
	<view class="container">
		<view class="back-btn yticon icon-zuojiantou-up" @click="navBack"></view>
		<view class="wrapper">
			<view class="left-top-sign">&nbsp;</view>
			<view class="welcome">
				商家认证
			</view>
			<view class="input-content">
				<view class="input-item">
					<text class="tit">商家名称</text>
					<input type="text" :value="businessName" placeholder="请输入商家名称" data-key="businessName"
						@input="inputChange" />
				</view>
				<view class="input-item">
					<text class="tit">营业执照编号</text>
					<input type="text" :value="businessLicense" placeholder="请输入营业执照编号" data-key="businessLicense"
						@input="inputChange" />
				</view>
				<view class="input-item">
					<text class="tit">法人身份证号码</text>
					<input type="number" :value="legalId" placeholder="请输入法人身份证号码" data-key="legalId"
						@input="inputChange" />
				</view>
			</view>
			<button class="confirm-btn" @click="toSubmit" :disabled="submitting">提交认证</button>
		</view>
	</view>
</template>

<script>
	import {
		mapMutations
	} from 'vuex';

	export default {
		data() {
			return {
				businessName: '',
				businessLicense: '',
				legalId: '',
				submitting: false
			};
		},
		methods: {
			...mapMutations(['submitBusiness']),
			inputChange(e) {
				const key = e.currentTarget.dataset.key;
				this[key] = e.detail.value;
			},
			navBack() {
				uni.navigateBack();
			},
			toSubmit() {
				this.submitting = true;
				const {
					businessName,
					businessLicense,
					legalId
				} = this;
				// 数据验证模块
				if (!this.validateForm()) {
					this.submitting = false;
					return;
				}
				const sendData = {
					businessName,
					businessLicense,
					legalId
				};
				try {
					console.log(sendData.businessName);
					const result = uni.request({
						url: 'http://localhost:8089/business/verify', // 替换为你的后端URL
						method: 'POST',
						data: JSON.stringify(sendData),
						header: {
							'content-type': 'application/json' // 根据后端要求设置请求头
						},
						success: (res) => {
							console.log(res.data); // 这里将打印出实际的响应数据
							if (res.statusCode === 200) {
								const verification = res.data;
								if (verification && typeof verification === 'object') {
									this.submitBusiness(res.data);
									uni.navigateBack();
									this.$api.msg('认证提交成功');
								} else {
									this.$api.msg(res.data || '认证提交失败');
								}
							} else {
								this.$api.msg('认证提交失败');
							}
						},
						fail: (error) => {
							this.$api.msg('网络错误');
						}
					});
				} catch (error) {
					this.$api.msg(error.message || '网络错误');
				} finally {
					this.submitting = false;
				}
			},
			validateForm() {
				// 这里添加您的表单验证逻辑
				// 返回true表示验证通过，返回false表示验证失败
				return true;
			}
		}
	}
</script>

<style lang='scss'>
	page {
		background: #fff;
	}

	.container {
		padding-top: 115px;
		position: relative;
		width: 100vw;
		height: 100vh;
		overflow: hidden;
		background: #fff;
	}

	.wrapper {
		position: relative;
		z-index: 90;
		background: #fff;
		padding-bottom: 40upx;
	}

	.back-btn {
		position: absolute;
		left: 40upx;
		z-index: 9999;
		padding-top: var(--status-bar-height);
		top: 40upx;
		font-size: 40upx;
		color: $font-color-dark;
	}

	.left-top-sign {
		font-size: 120upx;
		color: $page-color-base;
		position: relative;
		left: -16upx;
	}

	.welcome {
		position: relative;
		left: 50upx;
		top: -90upx;
		font-size: 46upx;
		color: #555;
		text-shadow: 1px 0px 1px rgba(0, 0, 0, .3);
	}

	.input-content {
		padding: 0 60upx;
	}

	.input-item {
		display: flex;
		flex-direction: column;
		align-items: flex-start;
		justify-content: center;
		padding: 0 30upx;
		background: $page-color-light;
		height: 120upx;
		border-radius: 4px;
		margin-bottom: 50upx;

		&:last-child {
			margin-bottom: 0;
		}

		.tit {
			height: 50upx;
			line-height: 56upx;
			font-size: $font-sm+2upx;
			color: $font-color-base;
		}

		input {
			height: 60upx;
			font-size: $font-base + 2upx;
			color: $font-color-dark;
			width: 100%;
		}
	}

	.confirm-btn {
		width: 630upx;
		height: 76upx;
		line-height: 76upx;
		border-radius: 50px;
		margin-top: 70upx;
		background: $uni-color-primary;
		color: #fff;
		font-size: $font-lg;

		&:after {
			border-radius: 100px;
		}
	}
</style>
